package com.codi.bus.core.service.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.codi.base.util.DateUtils;
import com.codi.base.util.MoneyUtils;
import com.codi.bus.core.resultModel.BaseResult;
import lombok.Data;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 组合
 * Created by asus on 2017/11/7.
 */
@Data
@Accessors(chain = true)
public class PortfolioModel extends BaseResult{

    private static final long serialVersionUID = 1L;

    /**
     * 组合包含的基金
     */
    private List<AssetFundModel> portfolioFunds;

    /**
     * 按交易账号区分的基金总额份额 交易账号
     */
    private List<PortfolioTradeModel> portfolioTrades;

    /**
     * 交易账号
     */
    private String tradeAcco;

    /**
     * 本组合资产
     */
    private BigDecimal portfolioWorthValue;

    public String getPortfolioWorthValueString() {
        return MoneyUtils.formatMoney(this.portfolioWorthValue, 2);
    }


    /**
     * 本组合当日收益
     */
    private BigDecimal portfolioTodayIncome;

    public String getPortfolioTodayIncomeString() {
        return MoneyUtils.formatMoney(this.portfolioTodayIncome, 2);
    }

    /**
     * 组合持仓收益
     */
    private BigDecimal portfolioShareIncome;
    public String getShareIncomeString() {
        return MoneyUtils.formatMoney(this.portfolioShareIncome, 2);
    }

    /**
     * 组合累积收益
     */
    private BigDecimal portfolioAccumIncome;
    public String getAccumIncomeString() {
        return MoneyUtils.formatMoney(this.portfolioAccumIncome, 2);
    }



    /**
     * 组合编号
     */
    private String portfolioCode;

    /**
     * 组合名
     */
    private String protocolName;

    /**
     * 分享组合的文案
     */
    private String share;


    /**
     * 是否有在途
     */
    @JSONField(serialize = false)
    private boolean inTransit;

    /**
     * 赎回在途笔数
     */
    private Integer sellInTransit;

    public String getSellInTransitString() {
        if (sellInTransit != null && sellInTransit != 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("有");
            sb.append(sellInTransit);
            sb.append("笔赎回确认中");
            return sb.toString();
        }
        return "";
    }

    /**
     * 调仓记录表ID
     */
    private Long transferId;

    /**
     * 调仓 0：不可调 1：待调仓 2：调仓中
     */
    private Integer stockUp;

    /**
     * 在途金额
     */
    @JSONField(serialize = false)
    private BigDecimal inTransitAmount;

    /**
     * 能否赎回
     */
    private boolean canSell;

    /**
     * 在途中笔数
     */
    @JSONField(serialize = false)
    private Integer inTransitNum;

    @JSONField(serialize = false)
    public String getInTransitNumString() {
        if (inTransitNum != null && inTransitNum != 0) {
            StringBuffer sb = new StringBuffer();
            sb.append("有");
            sb.append(inTransitNum);
            sb.append("笔买入确认中");
            return sb.toString();
        }
        return "";
    }

    public BigDecimal getCurrentShare(){
        BigDecimal currentShare = null;
        if (portfolioFunds != null){
            for (AssetFundModel assetFundModel : portfolioFunds){
                currentShare = MoneyUtils.add(currentShare,assetFundModel.getCurrentShares());
            }
        }
        return currentShare;
    }

    public BigDecimal getEnableShare(){
        BigDecimal enableShare = null;
        if (portfolioFunds != null) {
            for (AssetFundModel assetFundModel : portfolioFunds) {
                enableShare = MoneyUtils.add(enableShare, assetFundModel.getEnableShares());
            }
        }
        return enableShare;
    }

    /**
     * 净值日期
     */
    @JSONField(serialize = false)
    private Date netValueDate;

    // 净值日期字符串 MM-dd
    public String getNetValueDateString() {
        if (this.netValueDate != null) {
            return DateUtils.formatDate7(this.netValueDate);
        } else {
            return "";
        }
    }
}
